<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 内存淘汰机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .strategy-card {
            background: white;
            border-radius: 16px;
            padding: 24px;
            position: relative;
            overflow: hidden;
        }
        
        .strategy-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Redis 内存淘汰机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    深入理解缓存系统的核心策略
                </p>
                <div class="flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-memory mr-2"></i>
                        内存管理
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-chart-line mr-2"></i>
                        性能优化
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                        <i class="fas fa-shield-alt mr-2"></i>
                        系统稳定
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">R</span>edis 中的淘汰机制是为了在内存不足时，有效管理数据的过期与淘汰以保障系统的稳定性。当内存达到极限时，Redis 需要通过智能的淘汰策略来保证缓存中存放的都是热数据，及时清理冷数据，释放内存空间。
                </p>
            </div>
        </div>
    </section>

    <!-- Visual Overview -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-3xl font-bold text-center mb-12 text-gradient">淘汰策略全景图</h2>
                <div class="bg-white rounded-2xl shadow-xl p-8">
                    <div class="mermaid">
                        graph TD
                            A[Redis 内存淘汰机制] --> B[基于使用频率]
                            A --> C[基于时间]
                            A --> D[其他策略]
                            
                            B --> E[LRU 最近最少使用]
                            B --> F[LFU 最不经常使用]
                            
                            C --> G[TTL 生存时间]
                            C --> H[定期删除]
                            
                            D --> I[随机淘汰]
                            D --> J[noeviction 不淘汰]
                            
                            style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                            style B fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                            style C fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                            style D fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Strategy Details -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="text-4xl font-bold text-center mb-16">核心淘汰策略详解</h2>
                
                <!-- LRU -->
                <div class="mb-16">
                    <h3 class="section-title text-2xl font-bold mb-8">LRU (Least Recently Used) 最近最少使用</h3>
                    <div class="grid md:grid-cols-2 gap-8">
                        <div class="strategy-card card-hover">
                            <div class="icon-wrapper">
                                <i class="fas fa-clock"></i>
                            </div>
                            <h4 class="text-xl font-semibold mb-4">工作原理</h4>
                            <p class="text-gray-700 leading-relaxed">
                                LRU 算法是一种基于使用时间的淘汰机制。当内存不足以存储新数据时，系统会淘汰最近最少使用的数据。Redis 通过记录键的访问时间，使用 LRU 算法判断哪些键最久未被使用，然后将其淘汰。
                            </p>
                        </div>
                        <div class="strategy-card card-hover">
                            <div class="icon-wrapper">
                                <i class="fas fa-cog"></i>
                            </div>
                            <h4 class="text-xl font-semibold mb-4">应用场景</h4>
                            <ul class="space-y-2 text-gray-700">
                                <li><i class="fas fa-check text-green-500 mr-2"></i>热点数据缓存</li>
                                <li><i class="fas fa-check text-green-500 mr-2"></i>会话管理</li>
                                <li><i class="fas fa-check text-green-500 mr-2"></i>页面缓存</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- LFU -->
                <div class="mb-16">
                    <h3 class="section-title text-2xl font-bold mb-8">LFU (Least Frequently Used) 最不经常使用</h3>
                    <div class="grid md:grid-cols-2 gap-8">
                        <div class="strategy-card card-hover">
                            <div class="icon-wrapper">
                                <i class="fas fa-chart-bar"></i>
                            </div>
                            <h4 class="text-xl font-semibold mb-4">工作原理</h4>
                            <p class="text-gray-700 leading-relaxed">
                                LFU 算法是一种基于使用频率的淘汰机制。它考虑键的使用频率，将使用频率最低的键淘汰。Redis 通过计数器记录键的使用次数，并在需要淘汰数据时选择使用频率最低的键进行淘汰。
                            </p>
                        </div>
                        <div class="strategy-card card-hover">
                            <div class="icon-wrapper">
                                <i class="fas fa-tasks"></i>
                            </div>
                            <h4 class="text-xl font-semibold mb-4">优势特点</h4>
                            <ul class="space-y-2 text-gray-700">
                                <li><i class="fas fa-star text-yellow-500 mr-2"></i>更准确地识别热点数据</li>
                                <li><i class="fas fa-star text-yellow-500 mr-2"></i>避免偶发性访问影响</li>
                                <li><i class="fas fa-star text-yellow-500 mr-2"></i>长期稳定的缓存效果</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- TTL -->
                <div class="mb-16">
                    <h3 class="section-title text-2xl font-bold mb-8">TTL (Time To Live) 生存时间</h3>
                    <div class="strategy-card card-hover">
                        <div class="icon-wrapper">
                            <i class="fas fa-hourglass-half"></i>
                        </div>
                        <h4 class="text-xl font-semibold mb-4">机制说明</h4>
                        <p class="text-gray-700 leading-relaxed mb-4">
                            Redis 允许为每个键设置过期时间，即 TTL。一旦键过期，系统会自动将其淘汰。这种方式适用于需要在一定时间内保留的临时数据，如缓存、验证码、临时会话等。
                        </p>
                        <div class="highlight-box">
                            <p class="font-semibold text-gray-800">
                                <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                                最佳实践：为不同类型的数据设置合理的 TTL，既能保证数据时效性，又能有效利用内存空间。
                            </p>
                        </div>
                    </div>
                </div>

                <!-- 定期删除 -->
                <div class="mb-16">
                    <h3 class="section-title text-2xl font-bold mb-8">定期删除策略</h3>
                    <div class="strategy-card card-hover">
                        <div class="icon-wrapper">
                            <i class="fas fa-sync-alt"></i>
                        </div>
                        <h4 class="text-xl font-semibold mb-4">执行机制</h4>
                        <p class="text-gray-700 leading-relaxed">
                            定期删除是一种简单的淘汰机制，系统会定期检查键的过期时间并删除已经过期的键。虽然这种方式不够智能，但可以在一定程度上释放内存，减少内存占用。
                        </p>
                    </div>
                </div>

                <!-- 随机淘汰 -->
                <div class="mb-16">
                    <h3 class="section-title text-2xl font-bold mb-8">随机淘汰</h3>
                    <div class="strategy-card card-hover">
                        <div class="icon-wrapper">
                            <i class="fas fa-random"></i>
                        </div>
                        <h4 class="text-xl font-semibold mb-4">适用场景</h4>
                        <p class="text-gray-700 leading-relaxed">
                